package com.tuanzi.loan.web.bean.xhh.settlementsystem;

import com.google.common.collect.Lists;
import com.starbaba.model.utils.JSONUtils;
import com.tuanzi.loan.business.entity.xhh.settlementsystem.XhhSettlementBill;
import com.tuanzi.loan.business.entity.xhh.settlementsystem.XhhSettlementCompany;
import com.tuanzi.loan.business.entity.xhh.settlementsystem.XhhSettlementIncome;
import com.tuanzi.loan.business.entity.xhh.settlementsystem.XhhSettlementPrepay;
import com.tuanzi.loan.business.service.xhh.settlementsystem.XhhRemainType;
import com.tuanzi.loan.business.service.xhh.settlementsystem.XhhSettlementBillService;
import com.tuanzi.loan.business.service.xhh.settlementsystem.XhhSettlementCompanyService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.xhh.settlementsystem.XhhSettlementBillRequest;
import com.tuanzi.loan.vo.response.xhh.settlementsystem.XhhSettlementBillResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.xhh.settlementsystem.XhhSettlementBillLazyDataModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.List;

import static com.tuanzi.loan.business.constants.DictionaryCode.*;

/**
 * @author caiwanlin
 * @create 2018-08-24 16:32
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class XhhSettlementBillListBean extends JSFListingBean<XhhSettlementBillRequest, XhhSettlementBillResponse> {
    @Autowired
    private XhhSettlementCompanyService xhhSettlementCompanyService;

    @Autowired
    private XhhSettlementBillService xhhSettlementBillService;

    private List<XhhSettlementCompany> companyList;

    private XhhSettlementBill settlementBill;

    private List<XhhSettlementIncome> incomeList;

    private List<XhhSettlementPrepay> prepayList;

    private Long billId;
    private String billImageUrl;

    @PostConstruct
    public void init() throws Exception {
        companyList = xhhSettlementCompanyService.getCompanyList();
        settlementBill = new XhhSettlementBill();
        incomeList = Lists.newArrayList();
        prepayList = Lists.newArrayList();
        initLazyDataModel();
    }

    private void initLazyDataModel() throws Exception {
        $ = new XhhSettlementBillRequest();
        lazyDataModel = new XhhSettlementBillLazyDataModel($);
    }

    public void onReset(){
        try {
            initLazyDataModel();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void onSee(Long id){
        settlementBill = xhhSettlementBillService.findOne(id);
        String detailList = settlementBill.getDetailList();
        if (settlementBill.getRecordType() == XhhRemainType.EXPEND.index){//消耗开票
            if (StringUtils.isNotBlank(detailList)){
                incomeList = JSONUtils.fromJsonArrayNew(detailList, XhhSettlementIncome.class);
            }
        }
        else if (settlementBill.getRecordType() == XhhRemainType.PREPAY.index){
            if (StringUtils.isNotBlank(detailList)){
                prepayList = JSONUtils.fromJsonArrayNew(detailList, XhhSettlementPrepay.class);
            }
        }
        execute("PF('billing').show();");
        update("billForm:bill-info-dialog");

    }

    public void onSave(){
        if (settlementBill.getId()!=null){
            xhhSettlementBillService.saveOrUpdate(settlementBill);
        }
    }


    public void handleBillImageUpload(FileUploadEvent event){
        UploadedFile file = event.getFile();
        String url = this.uploadImg(file);
        XhhSettlementBill one = xhhSettlementBillService.findOne(billId);
        one.setBillImageUrl(url);
        xhhSettlementBillService.saveOrUpdate(one);
        addInfoMessage("上传成功");
        execute("PF('billImage').hide();");
        update("billForm");
    }

    @Override
    protected List<String> getDictionaryCodes() {
        return  Lists.newArrayList(XHH_SETTLEMENT_SPREAD_CHANNEL,XHH_SETTLEMENT_INCOME_TYPE,
                XHH_SETTLEMENT_SPREAD_MODE, XHH_COOPERATION_MODE, XHH_SETTLEMENT_CYCLE,
                XHH_SETTLEMENT_BILLING_CONTENT,XHH_SETTLEMENT_BILL_TYPE,XHH_SETTLEMENT_PAY_MODE);
    }
}
